package cn.ococci.dm.Main.View;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.ococci.dm.Main.Adapter.TitleBarAdapter;
import cn.ococci.dm.Main.Tools.ScreenTool;
import cn.ococci_dm.R;


/**
 * Created by cyan on 15/9/21.
 * TitleBar视图的封装
 */
public class TitleBar extends FrameLayout implements  View.OnClickListener{

    private View bgView;
    private TextView captionText;
    private ImageView rightImage;
    private TextView rightText;
    private LinearLayout radioLy;
    private ImageView backImage; //返回按钮（独立）

    TitleBarAdapter adapter;

    private int curRadioIndex;  //当前选项卡位置
    private int color;

    public TitleBar(Context context){
        this(context, null);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.activity_title_bar,this,true);
        initView();
    }

    private void initView(){
        bgView = super.findViewById(R.id.bgView);
        captionText = (TextView) super.findViewById(R.id.captionText);

        rightImage = (ImageView) super.findViewById(R.id.rightIv);
        rightText = (TextView) super.findViewById(R.id.rightTv);
        radioLy = (LinearLayout) super.findViewById(R.id.radioLy);
        backImage = (ImageView) super.findViewById(R.id.backIv);

        View[] allView = new View[]{ backImage, rightImage, rightText,captionText};
        for(View v:allView){
            v.setOnClickListener(this);
        }

        captionText.setTextColor(Color.WHITE);
        rightText.setTextColor(Color.WHITE);

        bgView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

    }

    public void setAdapter(TitleBarAdapter adapter){
        this.adapter = adapter;
        updateView();
    }

    public void updateView(){
        if(adapter.getBackCaption() == null){
            backImage.setVisibility(VISIBLE);
        }else
            backImage.setVisibility(INVISIBLE);

        if(adapter.getRadioTitles() != null){
            captionText.setVisibility(GONE);
            radioLy.setVisibility(View.VISIBLE);
            //选项卡
            initRadios();
        }else if(adapter.getCaption() == null){
            captionText.setVisibility(GONE);
            radioLy.setVisibility(View.GONE);
            //搜索
        }else{
            radioLy.setVisibility(View.GONE);
            captionText.setVisibility(VISIBLE);
            //标题
            if(adapter.getCaptionSize() > 0){
                captionText.setTextSize(adapter.getCaptionSize());
            }
            captionText.setText(adapter.getCaption());
        }

        if(adapter.getRightImageResId() > 0){
            rightText.setVisibility(GONE);
            rightImage.setVisibility(VISIBLE);
            //图片事件的处理
            rightImage.setImageResource(adapter.getRightImageResId());
        }else{
            rightImage.setVisibility(GONE);
            rightText.setVisibility(VISIBLE);
            //文字的处理
            if(adapter.getRightTextSize() > 0){
                rightText.setTextSize(adapter.getRightTextSize());
            }
            rightText.setText(adapter.getRightText());
        }

    }

    private void initRadios(){
        radioLy.removeAllViews();
        color = Color.rgb(0, 234, 218);
        for(int i=0;i<adapter.getRadioTitles().length;i++){
            if(i!=0){  //增加垂直的直线
                View view=new View(getContext());
                view.setBackgroundColor(color);
                radioLy.addView(view, i * 2 - 1, new LayoutParams(ScreenTool.dpToPx(1), ViewGroup.LayoutParams.MATCH_PARENT));
            }
            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
            int margin= ScreenTool.dpToPx(15);

            lp.setMarginStart(margin);
            lp.setMarginEnd(margin);

            TextView rb=new TextView(getContext());
            rb.setText(adapter.getRadioTitles()[i]);
            rb.setTextSize(17);
            rb.setGravity(Gravity.CENTER);
            rb.setPadding(margin, 0, margin, 0);
            rb.setTag(i);
            rb.setOnClickListener(radioClickListener);
            radioLy.addView(rb, i * 2, lp);
        }
        curRadioIndex = adapter.getRadioDefaultIndex();
        selectRadio();
    }

    public void selectRadio(){
        for(int i=0;i<radioLy.getChildCount();i++){
            if(i%2==0){
                TextView tv= (TextView) radioLy.getChildAt(i);
                tv.setTextColor(color);
                tv.setBackground(null);
            }
        }

        TextView tv= (TextView) radioLy.getChildAt(curRadioIndex*2);
        tv.setTextColor(Color.WHITE);

        GradientDrawable gd=new GradientDrawable();
        float rad = ScreenTool.dpToPx(6);
        if(curRadioIndex==0) {
            gd.setCornerRadii(new float[]{rad,rad, 0,0,0, 0, rad,rad});
        }else if(curRadioIndex==adapter.getRadioTitles().length-1){
            gd.setCornerRadii(new float[]{0, 0, rad, rad, rad, rad, 0, 0});
        }
        gd.setColor(color);
        tv.setBackground(gd);
    }

    public void initTitleBarTheme(int color){
        bgView.setBackgroundColor(color);
    }

    private OnClickListener radioClickListener=new OnClickListener() {
        @Override
        public void onClick(View v) {
            curRadioIndex=(Integer)v.getTag();
            selectRadio();
            adapter.onRadioClicked(curRadioIndex);
        }
    };

    public TitleBarAdapter getCurAdapter(){
        return this.adapter;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.backIv){  //返回
            adapter.onBackClick();
        }else if(id == R.id.rightTv){  //Right 文字的点击事件
            adapter.onRightTextClick();
        }else if(id == R.id.rightIv){  //Right 图片的点击事件
            adapter.onRightImageClick();
        }else if (id == R.id.captionText){  //caption 点击事件
            adapter.onCaptionClicked();
        }
    }





}
